<template>
	<view>
		<u-toast ref="uToast" />
		<view id="bg_box">
			<view @click="open" class="butt">{{ loading?'开启中...':'开启美好生活'}}</view>
		</view>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				loading:false
			}
		},
		methods:{
			open(){
				// 判断是否登录，有登录跳转首页，否则跳转选择性别
				if(this.$store.state.Authorization){
					uni.switchTab({
						url: '/pages/edge/edge'
					})
				}else{

					uni.navigateTo({
						url: '/pages/select_sex/select_sex'
					})
					
					// 小程序专用
					// this.wx_login()
				}
				
			},
			wx_login(){
				
				this.loading = true
				
				let that = this
				
				uni.openSetting({
						success(res){
							
							// 判断是否授权成功
							if(res.authSetting['scope.userInfo']){

								uni.login({
									provider:'weixin',
									success(res2){
										uni.getUserInfo({
											withCredentials:true,
										    success(res3) {
												that.$request({
													url:'/login/wxLogin',
													data:{code:res2.code,iv:res3.iv,rawData:res3.rawData,wx_signature:res3.signature,encryptedData:res3.encryptedData},
													method:'post',
													success(res){
														if(res.statusCode==200){
																
															if(res.data.code==1){
																that.$store.commit('changeLogin',res.data.data.token)
																
																that.$store.commit('setOpenid',res.data.data.openId)
																
																that.$store.commit('messageSocket')
																
																uni.switchTab({
																	url: '/pages/edge/edge'
																})
															}else{
																that.$refs.uToast.show({
																	title: res.data.msg,
																	type: 'warning'
																})
																
																that.error = true
															}
															
														}else{
															that.$refs.uToast.show({
																title: '网络繁忙',
																type: 'warning'
															})
															
															that.error = true
														}
													},
													fail(err){
														that.$refs.uToast.show({
															title: '请求失败',
															type: 'warning'
														})
														
														that.error = true
													},
													complete(){
														that.loading = false
													}
												})
												
										    },
										    fail() {
										       that.$refs.uToast.show({
										       	title: '开启失败',
										       	type: 'warning'
										       })
												
												that.error = true
												
												that.loading = false
										    }
										})
									},
									fail(err){
										that.error = true
										
										that.loading = false
									}
								})

							}
						},
						fail(err){

							uni.navigateTo({
								url: '/pages/select_sex/select_sex'
							})
							
						}
					})
			}
		}
	}
</script>

<style scoped>
	#bg_box{
		width: 750rpx;
		height: 100vh;
		background: url('https://wx.jianmeijiayuan.com/static/image/open_screen.png');
		background-size: 100% 100%;
	}
	#bg_box .butt{
		width: 620rpx;
		height: 100rpx;
		background-color: #EA4389;
		color:#fff;
		position: fixed;
		bottom: 10vh;
		left:65rpx;
		border-radius: 100rpx;
		text-align:center;
		line-height: 100rpx;
		font-size: 32rpx;
	}
</style>
